<?
	/* Goodreads.com - stream reader plugin */	

	// return type of service
	function goodreads_getType() { return "media"; }


	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function goodreads_getAction($line) {
		$entry=explode("|",$line);
		
		$entry[7]=trim($entry[7]);
		if ($entry[7]=="reads") return "<a href='?service=goodreads'><img class='icon' src='".serviceIcon("goodreads")."'/></a> I finished a book, see <a href='http://goodreads.com'>goodreads.com</a>";
		if ($entry[7]=="all") return "<a href='?service=goodreads'><img class='icon' src='".serviceIcon("goodreads")."'/></a> I added a book to <a href='http://goodreads.com'>goodreads.com</a>";
	
		//return "<img class='icon' src='".serviceIcon("goodreads")."'> I posted a book on <a href='http://goodreads.com'>goodreads.com</a>";
	}

	// extract and return the correct caption
	function goodreads_getCaption($line) {
		$entry=explode("|",$line);
		return $entry[6];	// no caption (yet) 
	}

	// extract and return the correct post
	function goodreads_getPost($line) {
		$entry=explode("|",$line);
		$itemPost="<img src='".$entry[3]."'/>";
		return $itemPost;
	}

	// extract and return the correct link to the item
	function goodreads_getLink($line) {
		$entry=explode("|",$line);
		return $entry[5];
	}	

	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function goodreads_identify($item) {
		$entry=explode("|",$item);

		if (strpos($entry[3],"ALL")) return "<a href='".$entry[3]."'>All books</a>";
		else if (strpos($entry[3],"review")) return "<a href='".$entry[3]."'>Read books</a>";

	}	
	
	// return entry form for new profiles
	function goodreads_form() {
		print "Profile url: <input type='text' name='username'> <input type='submit' name='add' value='add'><br/>";
		print "e.g., http://www.goodreads.com/user/show/1234<br/>";
		print "read books: <input type='radio' name='shelve' value='Read books' checked> ";
		print "all books: <input type='radio' name='shelve' value='All books'>";
		
	}
	

	// get service + profile information
	function goodreads_addProfile() {
	
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];

			$page = file_get_contents($username);

			// include reads
			if ($_POST['shelve']=="Read books") {
				$findlink=substr($page,strpos($page,"title=\"Bookshelves\""));
				$findlink=substr($findlink,strpos($findlink,"href=\"")+6);
				$findlink=substr($findlink,0,strpos($findlink,"\""));

				$rsslink=$findlink;
				writeService($serviceID,$username,$rsslink);
			}


			// include all
			if ($_POST['shelve']=="All books") {
				$findlink=substr($page,strpos($page,"title=\"Bookshelves\""));
				$findlink=substr($findlink,strpos($findlink,"href=\"")+6);
				$findlink=substr($findlink,0,strpos($findlink,"\""));

				$rsslink=$findlink."?shelf=%23ALL%23";
				writeService($serviceID,$username,$rsslink);
			}
	}
		
		
		
	
	function goodreads_download($serviceid,$filename)  {

		$reader = new XMLReader();
		$reader->open($filename );

			
		set_time_limit(30);


		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				
				if ($reader->nodeType==XMLREADER::ELEMENT) {

					if ($reader->localName=="item") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$content=null;
						
						while ($reader->read()) {

							if ($reader->name=="guid" && !$guid) {
								$reader->read();
								$guid=$reader->value;
							}

							if ($reader->localName=="book_large_image_url" && !$content) {
								while ($reader->read()) {
									if ($reader->nodeType==XMLREADER::CDATA) {
										$content=$reader->value;
										break;
									}
								}
							}
							
							if ($reader->localName=="pubDate" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
							}

							if ($reader->localName=="title" && !$title) {
								while ($reader->read()) {
									if ($reader->nodeType==XMLREADER::CDATA) {
										$title=$reader->value;
										break;
									}
								}
							}
		

							if ($reader->localName=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
								//$guid=$mediaurl;
							}
							
							if ($reader->localName=="item") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									
									if (substr($title,0,1)!="@" || $includeATtweets) {
										if (newGuid($guid)) {
											$item[$indx]->title=$title;
											$item[$indx]->post=$content;
											$item[$indx]->link=$link;
											$item[$indx]->pubDate=$pubdate;
											$item[$indx]->guid=$guid;
											$item[$indx]->extra="|".$link."|".$title."|";

											if (strpos($filename,"ALL")) {
												$item[$indx]->extra.="all|";
											} else if (strpos($filename,"review")) {
												$item[$indx]->extra.="reads|";
											}
											
										} else $continueParsing=false;
									}
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"goodreads",$serviceid);
	}
?>